package com.mrjy.maintain.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.AccessLog;
import com.mrjy.common.entity.CoursePackageInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.VideoInfo;
import com.mrjy.common.redis.RedisPool;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.coursepackage.dao.ICoursePackageInfoDao;
import com.mrjy.coursepackage.dao.ICoursePackageVipDao;
import com.mrjy.coursepackage.dto.CoursePackageInfoDTO;
import com.mrjy.coursepackage.dto.CoursePackageVipDTO;
import com.mrjy.maintain.bo.IAccessLogBo;
import com.mrjy.video.dao.IVideoVipDao;

@Controller
@RequestMapping("/maintain/accessLog")
public class AccessLogController {

	@Autowired
	private IAccessLogBo accessLogBo;
	@Autowired
	private RedisPool redisPool;
	@Autowired
	private IVideoVipDao videoVipDao;
	@Autowired
	private ICoursePackageVipDao coursePackageVipDao;
	@Autowired
	private ICoursePackageInfoDao coursePackageInfoDao;
	private Map<Object, Object> queryUserPaid;
	/**
	 * 分页查询访问日志
	 * 
	 * @param accessLog
	 * @return
	 */
	@RequestMapping(value = "/listByPage", method = RequestMethod.GET)
	@ResponseBody
	public Object list(AccessLog accessLog,HttpSession session) {
		return accessLogBo.listByPage(accessLog,session);
	}

	/**
	 * 导出excel
	 * 
	 * @param accessLog
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/export")
	@ResponseBody
	public Object export(HttpServletResponse response,AccessLog accessLog,HttpSession session){
		return accessLogBo.exportExl(accessLog,response,session);
	}
	
	@RequestMapping("/getTotal")
	@ResponseBody
	public Object getTotal(AccessLog accessLog){
		return accessLogBo.getTotal(accessLog);
	}
	
	
	/**
	 *  添加记录
	 *  type=1 登录/type=2 退出
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Object insert(HttpSession session, AccessLog accessLog){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		accessLogBo.txInsert(accessLog, userInfo);
		return "ok";
	}
	
	/**
	 * 在本地执行用
	 */
	@RequestMapping("/addRedis")
	@ResponseBody
	public void addRedis(){
		
		//查询出用户购买微课列表
		CopyOnWriteArrayList<Map<String, Object>> vList = new CopyOnWriteArrayList<>(videoVipDao.queryUserPaid());
		//课程包列表
		CopyOnWriteArrayList<Map<String, Object>> cList = new CopyOnWriteArrayList<>(coursePackageVipDao.queryUserPaid());
		for (Map<String, Object> cMap : cList) {
			String coursePackageName = (String) cMap.get("course_package_name");
			String[] cArr = coursePackageName.split(",");
			String vids = "";
			for (String cid : cArr) {
				CoursePackageInfo info = new CoursePackageInfo();
				info.setId(Integer.parseInt(cid));
				List<CoursePackageInfoDTO> videoList = coursePackageInfoDao.selectVideoListById(info);
				for (CoursePackageInfoDTO coursePackageInfoDTO : videoList) {
					vids += (coursePackageInfoDTO.getVideoId()+",");
				}
			}
			cMap.put("course_package_name", vids);
		}
		
		for (Map<String, Object> map1 : cList) {
			for (Map<String, Object> map2 : vList) {
				if (map1.get("user_id").toString().equals(map2.get("user_id").toString())) {
					//合并去重
					String result = combineArray(map1.get("course_package_name").toString(), map2.get("video_name").toString());
					redisPool.set(GlobalConstant.USER_VIDEO_VIP+map1.get("user_id"), result);
					cList.remove(map1);
					vList.remove(map2);
				}
			}
		}
		for (Map<String, Object> cmap : cList) {
			redisPool.set(GlobalConstant.USER_VIDEO_VIP+cmap.get("user_id"), cmap.get("course_package_name").toString());
		}
		for (Map<String, Object> vmap : vList) {
			redisPool.set(GlobalConstant.USER_VIDEO_VIP+vmap.get("user_id"), vmap.get("video_name").toString()+",");
		}
	}
	
	private String combineArray(String str1,String str2){
	    Set<String> set = new HashSet<String>();
	    if (StringUtils.isBlank(str1) || StringUtils.isBlank(str2)) {
			return "传入参数有空";
		}
	    for (String i : str1.split(",")) {
	        set.add(i);
	    }
	    for (String i : str2.split(",")) {
	        set.add(i);
	    }
	    List<String> list = new ArrayList<String>(set);
	    String result = "";
	    for (String string : list) {
			result += (string+",");
		}
	    return result;
	}
	
	
}
